package com.park.controller;

import com.park.bean.Manager;
import com.park.bean.Manager;
import com.park.common.CommonResult;
import com.park.service.IManagerService;
import com.park.util.JWTUtil;
import com.park.util.Page;
import org.apache.tomcat.util.http.parser.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class ManagerController {
    @Autowired
    IManagerService managerService;

    @PostMapping("dologin")
    public CommonResult doLogin(Manager manager){
        String token=managerService.doLogin(manager);
/*        if(token.equals("需要重置密码")){
            return  CommonResult.fail(302,"密码还未重置");
        }else */if(token==null){
            return CommonResult.fail(403,"登陆失败");
        }else{
            return CommonResult.success(200,"登陆成功",token);
        }
    }

    @GetMapping("/checktoken")
    public CommonResult checkToken(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        if(token!=null&& JWTUtil.verifyToken(token)){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }


    //展示和查询数据
    @GetMapping("manager")
    public CommonResult getManager(Manager manager, Page page){
        List<Manager> res=managerService.getManager(manager,page);
        int count=managerService.getCount(manager);
        HashMap map=new HashMap();
        map.put("res",res);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @GetMapping("checkuser")
    public CommonResult checkUserExist(Manager manager){
        System.out.println("这里检查用户名是否存在！！！！！！！！！！");
        boolean bool= managerService.checkUserExist(manager);
        if(bool){
            return CommonResult.success(200,"没有重复用户名");
        }else{
            return CommonResult.fail(401,"用户名已存在");
        }
    }
    @GetMapping("changecheck")
    public CommonResult changeCheck(Manager manager){
        boolean bool=managerService.changeCheck(manager);
        if(bool){
            return CommonResult.success(200,"用户名和密码一致");
        }else {
            return CommonResult.fail();
        }
    }




    //添加数据
    @PostMapping("manager")
    public CommonResult postManager(@RequestBody Manager manager){
        int count=managerService.postManager(manager);
        if(count>0){
            return CommonResult.success(manager);
        }else{
            return CommonResult.fail();
        }
    }

    //删除数据
    @DeleteMapping("manager/{id}")
    public CommonResult delManager(@PathVariable int id){
        int count= managerService.delManager(id);
        if(count>0){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }

    //修改数据
    @PutMapping("manager")
    public CommonResult editManager(@RequestBody Manager manager){
        int count=managerService.editManager(manager);
        if(count>0){
            return CommonResult.success(manager);
        }else{
            return CommonResult.fail();
        }
    }


    @PutMapping("change")
    public CommonResult changePass(@RequestBody Manager manager){
        int count= managerService.changePass(manager);
        if(count>0){
            return CommonResult.success(manager);
        }else{
            return CommonResult.fail();
        }
    }


}
